package Client;

import java.io.File;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Date;
import java.util.Scanner;

public class UDPClient {

	public String getBadgeID() {
		return badgeID;
	}

	private String badgeID;

	private static int officerCount = 1000;			

	//constructor of client,initialize the badgeID for client 
	public UDPClient(String stationCode) throws IOException {

		this.badgeID = stationCode + officerCount;
		createOfficerFile();
		officerCount++;
	}

	//creates officers log file
	private void createOfficerFile() throws IOException {
		File parentDir = new File("OfficerLogs");
		parentDir.mkdir();
		String fileName = getBadgeID() + ".txt";
		File file = new File(parentDir, fileName);
		file.createNewFile(); // Creates file in folder OfficerLogs

	}

	//print menu for user
	public static void showMenu() {
		System.out.println("\n**************************\n");
		System.out.println("Please select an option (1-7)");
		System.out.println("1. Create Criminal Record");
		System.out.println("2. Create Missing Record");
		System.out.println("3. Get Record Count");
		System.out.println("4. Edit Record");
		//System.out.println("5. Concurrency");
		System.out.println("6. Transfer record ");
		System.out.println("7. Exit");
	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub

		int userChoice = 0;
		int port=0;
		Scanner keyboard = new Scanner(System.in);
		showMenu();
		
		//take choice from user and validate choice
		while (true) {
			Boolean valid = false;

			//validate choice from user (only integer accepted)
			while (!valid) {
				try {
					userChoice = keyboard.nextInt();
					valid = true;
				} catch (Exception e) {
					System.out
							.println("Invalid Input, please enter an Integer");
					valid = false;
					keyboard.nextLine();
				}
			}
			
			
			DatagramSocket aSocket = null;
			try {
				aSocket = new DatagramSocket();
				StringBuilder sb=new StringBuilder();
				sb.append(Integer.toString(userChoice)+",");
				//String requestMessage = Integer.toString(userChoice)+",";
				byte[] m =new byte[10000];
				
				switch (userChoice) {
				
				//creates one client for SPVM and SPL each and creates criminal record
				case 1:
					UDPClient officer1 = new UDPClient("SPVM");
					sb.append("Durgesh,Vyas,Crime,false,"+
							officer1.getBadgeID());
					port=6879;
					break;
				case 2:
					UDPClient officer2 = new UDPClient("SPVM");
					sb.append("Himanshu,Patel,Crime,Montreal,"+new Date().toString()+",Rau,InProgress,"+
							officer2.getBadgeID());
					port=6879;

					break;
				case 3:
					//get record count for each server 
					UDPClient officer4 = new UDPClient("SPL");
					sb.append(officer4.getBadgeID());
					port=6880;
					break;
				case 4:
					//edit criminal record with recordID="CR10001"
					UDPClient officer5 = new UDPClient("SPVM");
					sb.append("Vyas,CR10000,Closed,"+officer5.getBadgeID());
					port=6879;
					break;
				case 5:
					
					break;
				case 6:
					UDPClient officer6 = new UDPClient("SPVM");
					sb.append(officer6.getBadgeID()+",CR10000,SPB");
					port=6879;
					break;
				case 7:
					System.out.println("Have a nice day!");
					keyboard.close();
					System.exit(0);
				default:
					System.out.println("Invalid Input, please try again.");
				}
				
				
				String requestMessage=sb.toString();
				sb=new StringBuilder();
				m=requestMessage.getBytes();
				InetAddress aHost = InetAddress.getByName("localhost");

				DatagramPacket request = new DatagramPacket(m, m.length, aHost,
						port);
				aSocket.send(request);
				byte[] buffer = new byte[10000];
				DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
				aSocket.receive(reply);
				String result = new String(reply.getData());
				System.out.println(result);
				//sb=null;
				requestMessage=null;
				m=new byte[10000];
				showMenu();

			

			} catch (SocketException e) {
				System.out.println("Socket:" + e.getMessage());
			} catch (IOException e) {
				System.out.println("IO:" + e.getMessage());
			} finally {
				if (aSocket != null)
					aSocket.close();
			}

		}

	}

}
